package com.bsy.framework.helper

import androidx.lifecycle.ViewModel
import androidx.lifecycle.ViewModelProvider
import com.bsy.framework.base.BaseApplication

/**
 * @author Sincerity
 * @date   2023/3/2 16:10
 * @desc   提供应用环境
 */
object AppHelper {
    private lateinit var app: BaseApplication
    private var isDebug = false
    private var mApplicationProvider: ViewModelProvider? = null
    fun init(application: BaseApplication, isDebug: Boolean) {
        app = application
        AppHelper.isDebug = isDebug
    }

    fun getBaseUrl(): String {
        return if (isDebug) return "https://dev-hwhome.skysrt.com"
        else "https://dev-hwhome.skysrt.com"
    }

    /**
     * 获取全局应用
     */
    fun getApplication() = app


    /**
     * 是否为debug环境
     */
    fun isDebug() = isDebug

    private fun getApplicationViewProvider(): ViewModelProvider {
        return ViewModelProvider(
            app.viewModelStore, ViewModelProvider.AndroidViewModelFactory.getInstance(app)
        )
    }

    /**
     * Application级别的Viewmodel
     *
     * @param T Viewmodel
     * @param modelClass Viewmodel类
     * @return 全局Viewmodel实例
     */
    fun <T : ViewModel> getApplicationViewModel(modelClass: Class<T>): T {
        if (mApplicationProvider == null) mApplicationProvider = getApplicationViewProvider()
        return mApplicationProvider!![modelClass]
    }
}